const ua = window.navigator.userAgent
const CHESPEED = !!ua.match(/360CHESPEED/i)
const CHENORMAL = !!ua.match(/360CHE/i) && !CHESPEED
console.log(CHESPEED)
console.log(CHENORMAL)
const isIOS = !(/android/i).test(ua)
class CallNativeMethod {
  constructor () {
    this.timer = 0
    this.method = ''
    this.param = ''
    this.callback = ''
  }

  setParams (method, param, callback) {
    this.method = method || ''
    this.param = param || ''
    this.callback = callback || ''
  }

  // normal-android:js->native
  adrCallNativeMethod () {
    if (window.WebViewJavascriptBridge) {
      return this.callback(window.WebViewJavascriptBridge)
    } else {
      document.addEventListener(
        'WebViewJavascriptBridgeReady',
        function () {
          return this.callback(window.WebViewJavascriptBridge)
        },
        false
      )
    }
  }

  // speed-android:js->native
  speedAdrCallNativeMethod () {
    try {
      if (JSON.stringify(this.param) === '{}' || !this.param) {
        // eslint-disable-next-line
        thSpeed[this.method]()
      } else {
        // eslint-disable-next-line
        thSpeed[this.method](typeof this.param === 'object' ? JSON.stringify(this.param) : this.param)
      }
    } catch (e) {
      console.log(e)
    }
  }

  // speed-callNativeMethod
  speedCallNativeMethod () {
    if (isIOS) {
      this.normalCallNativeMethod()
    } else {
      this.speedAdrCallNativeMethod()
    }
  }

  // normal-all & ios:js->native
  normalCallNativeMethod () {
    clearInterval(this.timer)
    this.timer = setInterval(() => {
      if (window.WebViewJavascriptBridge) {
        window.WebViewJavascriptBridge.callHandler(this.method, this.param, this.callback)
        clearInterval(this.timer)
      }
    }, 50)
  }

  callNativeMethod (method, param, callback) {
    this.setParams(method, param, callback)
    if (CHESPEED) {
      this.speedCallNativeMethod()
    }
    if (CHENORMAL) {
      this.normalCallNativeMethod()
    }
  }
}
class ConnectWebViewJavascriptBridge {
  constructor () {
    this.callback = () => { }
  }

  setParams (callback) {
    if (callback) {
      this.callback = callback
    }
  }

  speedRegisterAdr () {
    this.callback()
  }

  // speed --ios
  speedRegisterIOS () {
    const timer = setTimeout(() => {
      if (window.WVJBCallbacks) {
        timer && clearTimeout(timer)
        return window.WVJBCallbacks.push(this.callback)
      }
      window.WVJBCallbacks = [this.callback]
      var WVJBIframe = document.createElement('iframe')
      WVJBIframe.style.display = 'none'
      WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__'
      document.documentElement.appendChild(WVJBIframe)
      setTimeout(function () {
        document.documentElement.removeChild(WVJBIframe)
      }, 0)
      timer && clearTimeout(timer)
      this.speedRegisterIOS()
    }, 50)
  }

  // normal --ios/android
  normalConnect () {
    console.log('普通回调')
    const that = this
    if (window.WebViewJavascriptBridge) {
      return this.callback(window.WebViewJavascriptBridge)
    } else {
      document.addEventListener(
        'WebViewJavascriptBridgeReady',
        function () {
          return that.callback(window.WebViewJavascriptBridge)
        },
        false
      )
    }
  }

  speedConnect () {
    if (isIOS) {
      this.speedRegisterIOS()
    } else {
      this.speedRegisterAdr()
    }
  }

  connectWebViewJavascriptBridge (callback) {
    this.setParams(callback)
    if (CHESPEED) {
      this.speedConnect()
    }
    if (CHENORMAL) {
      this.normalConnect()
    }
  }
}
export const callHanlder = new CallNativeMethod()
export const connectBridge = new ConnectWebViewJavascriptBridge()
export const CHEAPPSIGN = {
  speed: CHESPEED,
  normal: CHENORMAL
}
